package filter;

import user.Order;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

//处理myorder.jsp中提交订单为空而引发空指针异常的问题
public class TrueorderFilter implements Filter {
    public void destroy() {
    }

    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
        HttpServletRequest request = (HttpServletRequest) req;
        HttpServletResponse response = (HttpServletResponse) resp;
        HttpSession session = request.getSession();
        List<Order> orders= (List<Order>) session.getAttribute("orderCar");
        if(orders.size()>0){
            chain.doFilter(request, response);
        }else {
            response.getWriter().println("请先添加商品！！！（2秒后自动跳转）");
            response.setHeader("refresh","2;URL=shop.jsp");
        }

    }

    public void init(FilterConfig config) throws ServletException {

    }

}
